clear all

global orig_data_thecb "E:\projects\142-RankII\NewFilesReleased\THECB\"
global orig_data "E:\projects\142-RankII\NewFilesReleased\"
global orig_data_tea "E:\projects\142-RankII\NewFilesReleased\TEA\"

global work_dir  "E:\projects\142-RankII\workfiles\"
global ui_dir "E:\projects\142-RankII\temp\"
global logfiles "E:\projects\142-RankII\logfiles\"
global dofiles "E:\projects\142-RankII\dofiles\"
global results "E:\projects\142-RankII\results\"

use $ui_dir/pre_analysis,replace

********************************************************************************
*Appendix Figure 1 - Similar schools
********************************************************************************

*Similar schools
sort y3_sch_coh_sub
by y3_sch_coh_sub: egen sch_max=max(raw_math_3)  
by y3_sch_coh_sub: egen sch_min=min(raw_math_3)
gen sch_range=sch_max-sch_min

by y3_sch_coh_sub: egen sch_mean=mean(raw_math_3)

foreach x in sch_max sch_min sch_range sch_mean{
	replace `x' = round(`x',1)
}

gen sch_min_demean=sch_mean-sch_min
gen sch_demean=raw_math_3-sch_mean

sort y3_sch_coh_sub
by y3_sch_coh_sub: gen SSC_size=_N

egen same_sch=group(sch_mean sch_min sch_max sch_range)

duplicates tag same_sch, gen(duplicates)

gen copy =duplicates/SSC_size

sort  duplicates same_sch  campus_3

*For FERPA PERTUBATE STUDENT TEST SCORES
drop sch_mean sch_demean
sort y3_sch_coh_sub

sum raw_math_3
gen pert_raw_math_3=raw_math_3+rnormal(0,.7049224)

encode campus_3, gen(school)

scatter schoool pert_raw_math_3  if same_sch==FERPA RESTRICTED

********************************************************************************
*Appendix Figure 2 - Common Support for Rank by Test Score
********************************************************************************
gen temp=1 if pert_raw_math_3!=.
egen pert_count=sum(temp), by(y3_sch_coh_sub)
egen pert_cl_rank_3_all=rank(pert_raw_math_3) if pert_raw_math_3!=., by(y3_sch_coh_sub)
gen pert_cl_prank_3_all=((pert_cl_rank_3_all-1)/(pert_count-1))


by y3_sch_coh_sub: egen sch_mean=mean(pert_raw_math_3) if pert_raw_math_3!=. & pert_cl_prank_3_all!=.
gen sch_demean=pert_raw_math_3-sch_mean
*Subject 1 =Maths
*Subject 2 =Reading
drop if subject==2
duplicates drop pert_cl_prank_3_all sch_demean SSC_size subject,force
scatter pert_cl_prank_3_all sch_demean if SSC_size>10 & subject==1,  ylab(0(0.25)1)  msize(vtiny) mcolor(black) xline(0,  lcolor(bluishgray)) xline(5) graphregion(color(white)) ytitle(Class Percentile Rank in Grade 3 Math Test) xtitle(School De-meaned 3Yr Math Test Score)
*graph save $results/demean_dist_all_sch_pert,replace


xtile sch_demean_pct = sch_demean, nq(100)
keep if SSC_size>10 & subject==1
gen cl_prank_3_all_max=pert_cl_prank_3_all
gen cl_prank_3_all_90=pert_cl_prank_3_all
gen cl_prank_3_all_75=pert_cl_prank_3_all
gen cl_prank_3_all_50=pert_cl_prank_3_all
gen cl_prank_3_all_25=pert_cl_prank_3_all
gen cl_prank_3_all_10=pert_cl_prank_3_all
gen cl_prank_3_all_min=pert_cl_prank_3_all
  
collapse (max) cl_prank_3_all_max (p90) cl_prank_3_all_90 (p75) cl_prank_3_all_75 (p50) cl_prank_3_all_50 (p25) cl_prank_3_all_25 (p10) cl_prank_3_all_10 (min) cl_prank_3_all_min (mean) sch_demean, by(sch_demean_pct)


line cl_prank_3_all_90 sch_demean || line  cl_prank_3_all_75 sch_demean || line  cl_prank_3_all_50 sch_demean || line  cl_prank_3_all_25 sch_demean || line  cl_prank_3_all_10 sch_demean ,  ylab(0(0.25)1)   xline(0,  lcolor(bluishgray)) xline(5) graphregion(color(white)) ytitle(Class Percentile Rank in Grade 3 Math Test) xtitle(School De-meaned 3Yr Math Test Score)
graph save $results/demean_dist_all_sch_line_pert,replace


********************************************************************************
*Appendix Figure 3 
********************************************************************************

use $ui_dir/pre_analysis,replace

tab ethnic,gen(ethnic_dum)

*ethnic_dum1 
global controls male fsme_dum esl_dum  ethnic_dum2 ethnic_dum3 ethnic_dum4 ethnic_dum5

char vent_cl_prank_ontime[omit] 10
char vent_st_prank_ontime[omit] 10

char vent_cl_prank_all[omit] 10
char vent_st_prank_all[omit] 10

char ethnic_dum[omit] 1


egen y3_sch_sub=group(campus_3 subject )
sort y3_sch_sub


graph set window fontface "Times New Roman"

sort y3_sch_sub
by y3_sch_sub: egen class_rank_stmedian=mean(cl_prank_3_all)   if st_prank_3_all>=0.495 & st_prank_3_all<0.505

sort   class_rank_stmedian y3_sch_sub
by class_rank_stmedian y3_sch_sub: gen test=1 if _n==1
gen test2=sum(test)
replace test2=.  if class_rank_stmedian==.

preserve
keep if st_prank_3_all>=0.495 & st_prank_3_all<0.505
xtile sch_rank = test2, nq(100)
 
 
gen cl_prank_3_all_max=cl_prank_3_all
gen cl_prank_3_all_90=cl_prank_3_all
gen cl_prank_3_all_75=cl_prank_3_all
gen cl_prank_3_all_50=cl_prank_3_all
gen cl_prank_3_all_25=cl_prank_3_all
gen cl_prank_3_all_10=cl_prank_3_all
gen cl_prank_3_all_min=cl_prank_3_all
  
collapse (max) cl_prank_3_all_max (p90) cl_prank_3_all_90 (p75) cl_prank_3_all_75 (p50) cl_prank_3_all_50 (p25) cl_prank_3_all_25 (p10) cl_prank_3_all_10 (min) cl_prank_3_all_min, by(sch_rank)

twoway lowess cl_prank_3_all_90 sch_rank  , lcolor(gs10) || lowess  cl_prank_3_all_75 sch_rank   , lcolor(gs5) || lowess  cl_prank_3_all_50 sch_rank  , lcolor(black) || lowess  cl_prank_3_all_25 sch_rank  , lcolor(gs5) || lowess  cl_prank_3_all_10 sch_rank , lcolor(gs10) graphregion(color(white)) ytitle(Rank in Class of 50th State Percentile) xtitle(Elementary-School-Subject Group) legend(off) yscale(r(0(0.1)1)) ylabel(#10)
graph save "$results/50 Support_sort_group_line mono.gph",replace



********************************************************************************
*Appendix Figure 5 - Common Support for Rank by Test Score by Distribution Type
********************************************************************************
clear
*Doing by school distribution
use $ui_dir/analysis,replace

*PERTUBATE STUDENT TEST SCORES
cap drop sch_mean sch_demean
sort y3_sch_coh_sub

sum raw_math_3
gen pert_raw_math_3=raw_math_3+rnormal(0,.7049224)


gen temp=1 if pert_raw_math_3!=.

egen pert_count=sum(temp), by(y3_sch_coh_sub)

egen pert_cl_rank_3_all=rank(pert_raw_math_3) if pert_raw_math_3!=., by(y3_sch_coh_sub)

gen pert_cl_prank_3_all=((pert_cl_rank_3_all-1)/(pert_count-1))

 
by y3_sch_coh_sub: egen sch_mean=mean(pert_raw_math_3) if pert_raw_math_3!=. & pert_cl_prank_3_all!=.
gen sch_demean=pert_raw_math_3-sch_mean
*Subject 1 =Maths
*Subject 2 =Reading
drop if subject==2


sort y3_sch_coh_sub
by y3_sch_coh_sub: gen SSC_size=_N

keep if SSC_size>10
duplicates drop pert_cl_prank_3_all sch_demean  subject school_mean_var,force
twoway scatter pert_cl_prank_3_all sch_demean if SSC_size>10 & subject==1, by(school_mean_var)  ylab(0(0.25)1)  msize(vtiny) mcolor(black) xline(0,  lcolor(bluishgray)) xline(5) graphregion(color(white)) ytitle(Class Percentile Rank in Grade 3 Math Test) xtitle(School De-meaned 3Yr Math Test Score)
graph save $results/demean_dist_all_sch_pert_dist,replace



xtile sch_demean_pct = sch_demean, nq(100)

gen cl_prank_3_all_max=pert_cl_prank_3_all
gen cl_prank_3_all_90=pert_cl_prank_3_all
gen cl_prank_3_all_75=pert_cl_prank_3_all
gen cl_prank_3_all_50=pert_cl_prank_3_all
gen cl_prank_3_all_25=pert_cl_prank_3_all
gen cl_prank_3_all_10=pert_cl_prank_3_all
gen cl_prank_3_all_min=pert_cl_prank_3_all
  
collapse (max) cl_prank_3_all_max (p90) cl_prank_3_all_90 (p75) cl_prank_3_all_75 (p50) cl_prank_3_all_50 (p25) cl_prank_3_all_25 (p10) cl_prank_3_all_10 (min) cl_prank_3_all_min (mean) sch_demean, by(sch_demean_pct school_mean_var)


twoway line cl_prank_3_all_90 sch_demean || line  cl_prank_3_all_75 sch_demean || line  cl_prank_3_all_50 sch_demean || line  cl_prank_3_all_25 sch_demean || line  cl_prank_3_all_10 sch_demean , by(school_mean_var) ylab(0(0.25)1)   xline(0,  lcolor(bluishgray)) graphregion(color(white)) ytitle(Class Percentile Rank in Grade 3 Math Test) xtitle(School De-meaned 3Yr Math Test Score)
graph save $results/demean_dist_all_sch_line_pert_dist,replace


